package com.mjlf.shiro.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @ClassName MyRealm
 * @auther mjlf
 * @Date 2019/6/10 14:48
 * @Description TODO
 */
public class MyRealm implements Realm {

    @Override
    public String getName() {
        return "myRealm";
    }

    /**
     * 只支持 用户名/密码模式
     * @param authenticationToken
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken authenticationToken) {
        return authenticationToken instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String username = (String) authenticationToken.getPrincipal();
        String password =  new String((char[])authenticationToken.getCredentials());

        if(!username.equals("zhang")){
            throw new UnknownAccountException();//用户名错误
        }
        if(!password.equals("123")){
            throw new IncorrectCredentialsException();//密码错误
        }
        return new SimpleAuthenticationInfo(username, password, getName());
    }
}
